<template>
  <view class="container">
    <!-- 页面头部标题 -->
    <view class="new-page-header">
      <!-- 状态栏占位 -->
      <view class="new-status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
      <!-- 导航栏 -->
      <view class="new-nav-bar" :style="{ height: navBarHeight + 'px' }">
        <!-- 返回按钮 -->
        <view class="new-back-button" @click="goBack">
          <u-icon name="arrow-left" size="36" color="#ffffff"></u-icon>
        </view>
        <!-- 标题 -->
        <view class="new-header-title" :style="{ lineHeight: navBarHeight + 'px'}">提现</view>
      </view>
    </view>

    <!-- 提现表单 -->
    <view class="withdraw-form">
      <!-- 表单卡片 -->
      <view class="form-card">
        <!-- 提现到账 -->
        <view class="form-row">
          <text class="form-label">提现到账</text>
          <view class="form-value">
            <text class="account-name">微信零钱</text>
            <!-- <u-icon name="checkmark-circle-fill" size="48" color="#74b9ff"></u-icon> -->
          </view>
        </view>

        <!-- 提现金额 -->
        <view class="form-section">
          <view class="section-title">提现金额</view>
          <view class="amount-input">
            <text class="currency-symbol">¥</text>
            <input 
              class="amount-field" 
              type="digit" 
              placeholder="请输入提现金额" 
              v-model="withdrawAmount"
              @input="onAmountInput"
            />
          </view>
          <view class="balance-info">
            <text class="balance-text">可提现金额¥1000</text>
            <text class="all-withdraw" @click="withdrawAll">全部提现</text>
          </view>
        </view>
      </view>

      <!-- 提现按钮 -->
      <view class="submit-section">
        <view class="submit-btn" :class="{ disabled: !canSubmit }" @click="handleWithdraw">
          <text class="submit-text">确认提现</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import navbarMixin from '@/common/mixin/navbar.js'

export default {
  mixins: [navbarMixin],
  
  data() {
    return {
      // 选中的账户
      selectedAccount: 'wechat',
      // 提现金额
      withdrawAmount: '',
      // 可提现余额
      availableBalance: 1000
    }
  },

  computed: {
    /**
     * 是否可以提交
     */
    canSubmit() {
      const amount = parseFloat(this.withdrawAmount)
      return amount > 0 && amount <= this.availableBalance
    }
  },

  methods: {
    /**
     * 返回上一页
     */
    goBack() {
      uni.navigateBack({
        delta: 1
      })
    },

    /**
     * 选择提现账户
     */
    selectAccount(account) {
      this.selectedAccount = account
    },

    /**
     * 金额输入处理
     */
    onAmountInput(e) {
      let value = e.detail.value
      // 限制小数位数
      if (value.includes('.')) {
        const parts = value.split('.')
        if (parts[1] && parts[1].length > 2) {
          value = parts[0] + '.' + parts[1].substring(0, 2)
        }
      }
      this.withdrawAmount = value
    },

    /**
     * 全部提现
     */
    withdrawAll() {
      this.withdrawAmount = this.availableBalance.toString()
    },

    /**
     * 处理提现
     */
    handleWithdraw() {
      if (!this.canSubmit) {
        uni.showToast({
          title: '请输入正确的提现金额',
          icon: 'none'
        })
        return
      }

      const amount = parseFloat(this.withdrawAmount)
      uni.showModal({
        title: '确认提现',
        content: `确定要提现¥${amount.toFixed(2)}到微信零钱吗？`,
        success: (res) => {
          if (res.confirm) {
            uni.showLoading({
              title: '提现中...'
            })
            
            // 模拟提现过程
            setTimeout(() => {
              uni.hideLoading()
              uni.showToast({
                title: '提现申请已提交',
                icon: 'success'
              })
              
              // 提现成功后返回
              setTimeout(() => {
                uni.navigateBack({
                  delta: 1
                })
              }, 1500)
            }, 2000)
          }
        }
      })
    }
  },

  /**
   * 分享当前页面
   */
  onShareAppMessage() {
    return {
      title: '提现',
      path: "/pages/newwithdraw/index"
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background: #f8f9fa;
}

.withdraw-form {
  padding: 40rpx 30rpx;
  
  .form-card {
    background: #ffffff;
    border-radius: 20rpx;
    padding: 40rpx 30rpx;
    margin-bottom: 30rpx;
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
    
    .form-row {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 60rpx;
      
      .form-label {
        font-size: 28rpx;
        color: #333333;
      }
      
      .form-value {
        display: flex;
        align-items: center;
        gap: 16rpx;
        
        .account-name {
          font-size: 28rpx;
          color: #333333;
        }
      }
    }
    
    .form-section {
      .section-title {
        font-size: 28rpx;
        color: #333333;
        margin-bottom: 50rpx;
      }
      
      .amount-input {
        display: flex;
        align-items: center;
        margin-bottom: 60rpx;
        
        .currency-symbol {
          font-size: 80rpx;
          color: #333333;
          margin-right: 40rpx;
        }
        
        .amount-field {
          flex: 1;
          font-size: 40rpx;
          color: #333333;
          border: none;
          outline: none;
          
          &::placeholder {
            color: #cccccc;
            font-size: 32rpx;
          }
        }
      }
      
      .balance-info {
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        .balance-text {
          font-size: 26rpx;
          color: #666666;
        }
        
        .all-withdraw {
          font-size: 26rpx;
          color: #e74c3c;
        }
      }
    }
  }
  
  .submit-section {
    margin-top: 80rpx;
    
    .submit-btn {
      width: 100%;
      height: 100rpx;
      border-radius: 50rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      background: #cccccc;
      
      .submit-text {
        font-size: 32rpx;
        font-weight: 600;
        color: #ffffff;
      }
      
      &:not(.disabled) {
        background: #74b9ff;
        box-shadow: 0 8rpx 24rpx rgba(116, 185, 255, 0.4);
        
        &:active {
          transform: translateY(2rpx);
          box-shadow: 0 6rpx 20rpx rgba(116, 185, 255, 0.5);
        }
      }
    }
  }
}
</style> 